﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using GQ.Controls.Tools;
using GQ.BLL.QuanLyKho;
using System.Collections;
using GQ.Utils.TreeNode;
using GQ.WinForms;
using GQ.Utils;

namespace GQ.QuanLyKho
{
    public partial class frmPhongBan : frmBase
    {
        ValidateCollection _validates = new ValidateCollection();
        PhongBan _phongban = new PhongBan()
        {
            CompanyID = new Guid(Config.CompanyID),
            PhongBanChaID = Guid.Empty

        };
        public frmPhongBan()
        {
            InitializeComponent();
            TreeControls.SetReadOnly(treeList1);
            _validates.Add(new ValidateEmpty(txtMa, "Mã không được để trống", dxErrorProvider1));
            _validates.Add(new ValidateEmpty(txtTen, "Tên không được để trống", dxErrorProvider1));
            LoadPhongBan();
        }
        private void LoadPhongBan()
        {
            IList<PhongBan> phongs = PhongBan.LoadAllBy("CompanyID={0} and PhongBanChaID={1} Sortby Stt", Config.CompanyID, Guid.Empty);
            NodeCollection nodes = new NodeCollection();

            foreach (PhongBan item in phongs)
            {
                nodes.Add(item);
            }
            TreeControls.LoadDataTree(treeList1, nodes);
            cboPhongban.LoadDataTree(nodes);

        }
        private void SetPhongBan()
        {

            cboPhongban.SetValue(_phongban.PhongBanChaID.Value);
            txtMa.Text = _phongban.Ma;
            txtTen.Text = _phongban.Ten;
            txtSoDienThoai.Text = _phongban.DienThoai;
            txtDiaChi.Text = _phongban.DiaChi;
            txtFax.Text = _phongban.Fax;
            txtGhiChu.Text = _phongban.GhiChu;

        }
        private void GetPhongBan()
        {
            if (cboPhongban.Value != null)
            {
                PhongBan parent = (PhongBan)cboPhongban.Value;
                _phongban.PhongBanChaID = parent.ID;
            }
            else _phongban.PhongBanChaID = Guid.Empty;
            _phongban.Ma = txtMa.Text;
            _phongban.Ten = txtTen.Text;
            _phongban.DienThoai = txtSoDienThoai.Text;
            _phongban.DiaChi = txtDiaChi.Text;
            _phongban.Fax = txtFax.Text;
            _phongban.GhiChu = txtGhiChu.Text;

        }
        private void frmPhongBan_Load(object sender, EventArgs e)
        {

        }

        private void menuButton1_AddNewClick()
        {
            _phongban = new PhongBan()
            {
                CompanyID = new Guid(Config.CompanyID),
                PhongBanChaID = Guid.Empty
            };
            SetPhongBan();
        }
        private void Delete(PhongBan phongBan)
        {
            if (phongBan.Childs.Count > 0)
                foreach (PhongBan item in phongBan.Childs)
                {
                    Delete(item);
                }
            phongBan.Delete();
        }
        private void menuButton1_DeleteClick()
        {
            try
            {
                if (Box.Show("Bạn có muốn xóa phòng ban này không?", MessageBoxButtons.YesNo, MessageBoxIcon.Question) != DialogResult.Yes)
                    return;
                Delete(_phongban);
                Helpers.Project.SetStatus("Xóa thành công", StatusInfo.Complete);
                LoadPhongBan();
            }
            catch (Exception ex)
            {
                Helpers.Log(ex);
                Helpers.Project.SetStatus("Xóa không thành công", StatusInfo.Error);
            }
        }

        private void menuButton1_EditClick()
        {
            //_phongban= (PhongBan)treeList1.FocusedNode
            SetPhongBan();
        }

        private void menuButton1_SaveClick()
        {
            try
            {
                GetPhongBan();

                if (!_validates.Validate()) return;
                if (_phongban.PhongBanChaID == _phongban.ID)
                {
                    Box.Show("Phòng ban cha không hợp lệ", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    cboPhongban.Focus();
                    return;
                }
                _phongban.Save();
                Helpers.Project.SetStatus("Lưu thành công", StatusInfo.Complete);
                LoadPhongBan();

            }
            catch (Exception ex)
            {
                Helpers.Log(ex);
                Helpers.Project.SetStatus("Lưu không thành công", StatusInfo.Error);

            }
            menuButton1.Action = GQ.Controls.ActionState.View;
        }

        private void treeList1_AfterFocusNode(object sender, DevExpress.XtraTreeList.NodeEventArgs e)
        {
            _phongban = (PhongBan)e.Node.Tag;
            SetPhongBan();
            menuButton1.Action = GQ.Controls.ActionState.View;
        }



    }
}
